package com.ai.zhly.service;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ai.dmc.index.base.impl.base.BaseServiceImpl;
import com.ai.dmc.index.base.sequence.SequenceGenerator;
import com.ai.dmc.index.common.exception.BusinessException;
import com.ai.dmc.utils.json.JsonUtil;
import com.ai.frame.bean.InputObject;
import com.ai.frame.bean.OutputObject;
import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;

/**
 * 
 * @Description: 客户画像
 * @author likun
 * @version zhly2期
 * @date 2016.7.19
 */
public class TourisPortraitServiceImpl extends BaseServiceImpl
		implements ITourisPortraitService {
	private static final int TOPNUM = 5;// 地域TOP数
	private static final int TOPNUM_DEVICE = 8;// 手机品牌TOP数
	// 定义对应Mapper文件的命名空间
	private static final String NAMESPACE = "touristPortrait.";

	public static Logger log = LoggerFactory
			.getServiceLog(TourisPortraitServiceImpl.class);

	private SequenceGenerator sequenceGenerator;

	private ICommonService commonService;

	public void setSequenceGenerator(SequenceGenerator sequenceGenerator) {
		this.sequenceGenerator = sequenceGenerator;
	}

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	@Override
	public void queryAgeLevel(InputObject inputObject,
			OutputObject outputObject) throws BusinessException {
		List<Map<String, String>> lResult = getBaseDao().queryForList(
				NAMESPACE + "queryAgeLevel", inputObject.getParams());

		outputObject.setObject(JsonUtil.Object2Json(lResult));
	}

	@Override
	public void querySexualLevel(InputObject inputObject,
			OutputObject outputObject) throws BusinessException {
		Map<Object, Object> sexualResult = new HashMap<Object, Object>();
		List<Map<String, String>> lResult = getBaseDao().queryForList(
				NAMESPACE + "querySexualLevel", inputObject.getParams());
		String maleNum = "0";
		String femaleNum = "0";
		String unkownSexualNum = "0";
		// 遍历结果
		if (lResult.size() != 0) {
			for (Map<String, String> sexMap : lResult) {
				if ("1".equals(sexMap.get("sexual"))) {// 男
					maleNum = sexMap.get("vstPersonCnts");// 男性数目;
				}
				if ("2".equals(sexMap.get("sexual"))) {// 女
					femaleNum = sexMap.get("vstPersonCnts");// 女性数目;
				}
				if ("3".equals(sexMap.get("sexual"))) {
					unkownSexualNum = sexMap.get("vstPersonCnts");// 未知性别的数目
				}

			}
			double maleCount = Double.parseDouble(maleNum);
			double femaleCount = Double.parseDouble(femaleNum);
			double percentage = 1.0;
			if (femaleCount != 0.0) {
				percentage = maleCount / femaleCount;
			}
			// 算出男女比例
			double unkownSexualCount = Double.parseDouble(unkownSexualNum);
			// 按比例分配位置人数
			double unkownmalePart = unkownSexualCount * percentage;
			double unkownfemalePart = unkownSexualCount - unkownmalePart;
			long maleLastCount = Math.round(maleCount + unkownmalePart);
			long femaleLastCount = Math.round(femaleCount + unkownfemalePart);
			sexualResult.put("maleCount", maleLastCount);
			sexualResult.put("femaleCount", femaleLastCount);
			outputObject.setObject(JsonUtil.Object2Json(sexualResult));
		} else {
			sexualResult.put("maleCount", "0");
			sexualResult.put("femaleCount", "0");
			outputObject.setObject(JsonUtil.Object2Json(sexualResult));
		}
	}

	@Override
	public void queryProvLevel(InputObject inputObject,
			OutputObject outputObject) throws BusinessException {
		List<Map<String, String>> lResult = getBaseDao().queryForList(
				NAMESPACE + "queryProvLevel", inputObject.getParams());
		double sumProCount = 0;// 总数
		if (lResult.size() != 0) {
			for (Map<String, String> provMap : lResult) {
				sumProCount += Double.parseDouble(provMap.get("vstPersonCnts"));
			}
			DecimalFormat df = new DecimalFormat("##");
			for (int i = 0; i <= TOPNUM; i++) {
				// 取前五,且将总数转换成百分比
				double percentage = 1.0;
				double eachTemp = Double
						.parseDouble(lResult.get(i).get("vstPersonCnts"));
				if (sumProCount != 0.0) {
					percentage = eachTemp / sumProCount * 100;
				}

				lResult.get(i).put("vstPersonCnts", df.format(percentage));
			}
			if (lResult.size() < TOPNUM) {
				outputObject.setBeans(JsonUtil.Object2Json(lResult));
			} else {
				List<Map<String, String>> subList = lResult.subList(0, TOPNUM);
				outputObject.setBeans(JsonUtil.Object2Json(subList));
			}
		} else {

			outputObject.setBeans(JsonUtil.Object2Json(lResult));
		}
	}

	@Override
	public void queryCunsumeLevel(InputObject inputObject,
			OutputObject outputObject) throws BusinessException {
		List<Map<String, String>> lResult = getBaseDao().queryForList(
				NAMESPACE + "queryCunsumeLevel", inputObject.getParams());

		double sumProCount = 0;// 总数
		if (lResult.size() != 0) {
			for (Map<String, String> provMap : lResult) {
				sumProCount += Double.parseDouble(provMap.get("vstPersonCnts"));
			}
			DecimalFormat df = new DecimalFormat("##");
			for (int i = 0; i < lResult.size(); i++) {
				double percentage = 1.0;
				double eachTemp = Double
						.parseDouble(lResult.get(i).get("vstPersonCnts"));
				if (sumProCount != 0.0) {
					percentage = eachTemp / sumProCount * 100;
				}

				lResult.get(i).put("vstPersonCnts", df.format(percentage));
			}
			outputObject.setBeans(JsonUtil.Object2Json(lResult));
		} else {

			outputObject.setBeans(JsonUtil.Object2Json(lResult));
		}
	}

	@Override
	public void querydeviceNameLevel(InputObject inputObject,
			OutputObject outputObject) throws BusinessException {
		List<Map<String, String>> lResult = getBaseDao().queryForList(
				NAMESPACE + "querydeviceNameLevel", inputObject.getParams());

		double sumProCount = 0;// 总数
		if (lResult.size() != 0) {
			for (Map<String, String> provMap : lResult) {
				sumProCount += Double.parseDouble(provMap.get("vstPersonCnts"));
			}
			DecimalFormat df = new DecimalFormat("##");
			double suMPercentage = 0.0;
			for (int i = 0; i < lResult.size(); i++) {
				double percentage = 1.0;
				double eachTemp = Double
						.parseDouble(lResult.get(i).get("vstPersonCnts"));
				if (sumProCount != 0.0) {
					suMPercentage += eachTemp / sumProCount;
					percentage = eachTemp / sumProCount * 100;
				}

				lResult.get(i).put("vstPersonCnts", df.format(percentage));
			}
			if ((lResult.size()) <= TOPNUM_DEVICE) {
				Map<String, String> tmpMap = new HashMap<String, String>();
				tmpMap.put("vstPersonCnts",
						df.format((1 - suMPercentage) * 100));
				tmpMap.put("devicename", "其他");
				lResult.add(tmpMap);
				outputObject.setBeans(JsonUtil.Object2Json(lResult));
			} else {

				lResult.get(TOPNUM_DEVICE).put("vstPersonCnts",
						df.format((1 - suMPercentage) * 100));
				lResult.get(TOPNUM_DEVICE).put("devicename", "其他");
				List<Map<String, String>> subList = lResult.subList(0,
						TOPNUM_DEVICE + 1);
				outputObject.setBeans(JsonUtil.Object2Json(subList));
			}
		} else {

			outputObject.setBeans(JsonUtil.Object2Json(lResult));
		}
	}

}
